package org.example.likou150.circularList;

// https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/?envType=study-plan-v2&envId=top-interview-150
public class DeleteDuplicates {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode preHead = new ListNode(0);
        ListNode nextNode = preHead;
        while (head != null) {
            if(head.next !=null && head.val == head.next.val){
                while (head.next !=null && head.val == head.next.val) {
                    head = head.next;
                }
            }else {
                nextNode.next = head;
                nextNode = nextNode.next;
            }
            head = head.next;
        }
        nextNode.next = null;
        return preHead.next;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
